package com.hins.sort;

import java.util.Arrays;

public class ShellSort {
    public static void main(String[] args) {
        int[] arr = { 8, 9, 1, 7, 2, 3, 5, 4, 6, 0};
//        shellSort(arr);
        shellSort2(arr);
    }

    public static void shellSort(int[] arr) {
        int temp;
        int count;
        for (int gap = arr.length / 2; gap > 0; gap /= 2) {
            for (int i = gap; i < arr.length; i++) {
                for (int j = i - gap; j >= 0 ; j-=gap) {
                    if (arr[j] > arr[j+gap]) {
                        temp = arr[j];
                        arr[j] = arr[j+gap];
                        arr[j+gap] = temp;
                    }
                }
            }
            System.out.println("排序后的结果" + Arrays.toString(arr));
        }
    }

    public static void shellSort2(int[] arr) {
        for (int gap = arr.length / 2; gap > 0; gap/=2) {
            for (int i = gap; i < arr.length; i++) {
                int j = i;
                int temp = arr[j];
                if (arr[j] < arr[j-gap]) {
                    while (j - gap >= 0 && temp < arr[j-gap]) {
                        arr[j] = arr[j-gap];
                        j-=gap;
                    }
                    // 这个是不断向后复制，然后最小就是这个temp，然后把temp复制到这个arr[j]即可
                    arr[j] = temp;
                }
            }
            System.out.println("排序后的结果" + Arrays.toString(arr));
        }
    }
}
